package cn.youyeku.sockettransmission.ui.fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import com.obsez.android.lib.filechooser.ChooserDialog;

import java.io.File;

import cn.youyeku.customizeview.SettingText;
import cn.youyeku.sockettransmission.R;
import cn.youyeku.sockettransmission.ui.ConstantAppConfig;
import cn.youyeku.sockettransmission.utils.LogUtils;

import static cn.youyeku.sockettransmission.ui.ConstantAppConfig.IP;


/**
 * Created by youye on 17-6-1.
 */

public class ConfigureFragment extends BaseFragment {
    SettingText SettingIP;
    SettingText SettingPort;
    SettingText Setting_rec;
    Button start;
    Context context;


    //广播用于发送
    LocalBroadcastManager mLocalBroadcastManager;
    private static final int REQUEST_PERMISSION = 9999;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fgm_conf, null);

        SettingIP = (SettingText) view.findViewById(R.id.setting_ip);
        SettingPort = (SettingText) view.findViewById(R.id.setting_port);
        Setting_rec = (SettingText) view.findViewById(R.id.setting_rec);
        start = (Button) view.findViewById(R.id.start);
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());

        //接收文件目录
        Setting_rec.binding.settingValues.setText(ConstantAppConfig.PATH);
        SettingIP.binding.settingValues.setText(ConstantAppConfig.IP);
        SettingPort.binding.settingValues.setText(ConstantAppConfig.PORT+"");




        SettingIP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(getContext()).title("配置IP")
                        .negativeText("取消")

                        .inputType(InputType.TYPE_CLASS_TEXT)
                        //参数1：hint 参数2：自动填充的文字 参数3：监听
                        .input("填写IP", IP, new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                IP = input.toString();
                                SettingIP.binding.settingValues.setText(IP);
                                LogUtils.w("IP---------------");
                                ConstantAppConfig.readWriteXml.writeString("IP",IP).commit();


                            }
                        }).show();
            }
        });

        SettingPort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(getContext()).title("配置端口")
                        .negativeText("取消")
                        .inputType(InputType.TYPE_CLASS_TEXT)
                        //参数1：hint 参数2：自动填充的文字 参数3：监听
                        .input("提示", "", new MaterialDialog.InputCallback() {
                            @Override
                            public void onInput(MaterialDialog dialog, CharSequence input) {
                                ConstantAppConfig.PORT = Integer.parseInt(input.toString());
                                //todo 提示 这样写其他不好 我只是偷懒而已

                                SettingPort.binding.settingValues.setText(ConstantAppConfig.PORT + "");
                                ConstantAppConfig.readWriteXml.writeInt("PORT", ConstantAppConfig.PORT).commit();
                            }
                        }).show();
            }
        });


        Setting_rec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.w("--------", ConstantAppConfig.SAVE_PATH);
                new ChooserDialog().with(getActivity())
                        .withFilter(true, false)
                        .withStartFile(ConstantAppConfig.SAVE_PATH)
                        .withChosenListener(new ChooserDialog.Result() {
                            @Override
                            public void onChoosePath(String path, File pathFile) {
                                Toast.makeText(getActivity(), "FOLDER: " + path, Toast.LENGTH_SHORT).show();
                                ConstantAppConfig.SAVE_PATH = path;
                                ConstantAppConfig.PATH=path;
                                Setting_rec.binding.settingValues.setText(ConstantAppConfig.SAVE_PATH);
                                ConstantAppConfig.readWriteXml.writeString("PATH", ConstantAppConfig.PATH).commit();
                            }
                        })
                        .build()
                        .show();

            }
        });


        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (!ConstantAppConfig.isStart) {
                    mLocalBroadcastManager.sendBroadcast(new Intent("youyeku.initClient"));

                    start.setText("已经连接");
                }


            }
        });

        request_permission();

        return view;
    }

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted.
            } else {
                // User refused to grant permission.
            }
        }
    }

    public void request_permission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, REQUEST_PERMISSION);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
